<?php


namespace BoojasrFramework\Response;


use BoojasrFramework\ServicesContainer\ServicesClassFactory;
use Twig\Environment;

class ViewResponse extends Response
{

    public function __construct($view = '',$data=[], int $status = 200, array $headers = [])
    {
        $this->content = "";
        try {
            $this->content = $this->getTwig()->render($view,$data);
        }catch (\Exception $e){
            $this->content = "页面渲染异常";
            $status = 500;
        }
        parent::__construct($this->content, $status, $headers);
    }

    /**
     * @return Environment
     */
    public function getTwig(){
        static $twig;
        if (!$twig){
            /** @var Environment $twig */
            $twig = ServicesClassFactory::$container->get("twig")->getTwig();
        }
        return $twig;
    }
}
